package org.tom.quartz.job;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.common.models.AlipayTradeCloseResponse;
import lombok.SneakyThrows;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.tom.order.domain.OrderPet;
import org.tom.order.domain.OrderPetDetail;
import org.tom.order.mapper.OrderPetDetailMapper;
import org.tom.order.mapper.OrderPetMapper;
import org.tom.pay.domain.PayBill;
import org.tom.pay.mapper.PayBillMapper;
import org.tom.pet.domain.Pet;
import org.tom.pet.mapper.PetMapper;
import org.tom.quartz.util.QuartzUtil;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Component
public class OrderExpireJob implements Job {
    
    @Autowired
    private PetMapper petMapper;
    
    @Autowired
    private Scheduler scheduler;

    @Autowired
    private OrderPetMapper orderPetMapper;

    @Autowired
    private OrderPetDetailMapper orderPetDetailMapper;

    @Autowired
    private PayBillMapper payBillMapper;
    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
        Map<String,String> params = (Map) jobDataMap.get("params");
        String sn = params.get("sn");
        PayBill payBill = payBillMapper.loadBySn(sn);
        // 通过订单号码查询订单，要是不是待支付，就说明已经处理了订单就删除定时任务
        if (payBill.getState() != PayBill.WAIT_PAY){
            QuartzUtil.removeJob(scheduler, sn);
            return;
        }

        // 通过订单编号查询业务单
        // 已经支付过后就直接删除定时任务
        // 在同步回调过程中修改数据库字段resultUrl（补充在地址栏中%s可以使用format替换占位符）
        OrderPet orderPet = orderPetMapper.loadBySn(sn);
        if (orderPet.getState() != OrderPet.WAIT_PAY_ORDER){
            QuartzUtil.removeJob(scheduler, sn);
            return;
        }

        // 通过订单编号查询订单明细
        List<OrderPetDetail> pets = orderPetDetailMapper.loadBySn(sn);
        // 流式操作将集合中的所有对象的id取出来
        List<Long> petIds = pets.stream().map(pet -> pet.getPetId()).collect(Collectors.toList());

        // 修改支付单的状态状态
        payBill.setState(PayBill.SATET_OUTTIME_OR_RETURN);
        payBillMapper.update(payBill);

        // 修改业务单的状态
        orderPet.setState(OrderPet.FAIL_PAY_BYTIME_ORDER);
        orderPetMapper.update(orderPet);

        // 宠物的库存添加
        List<Pet> pets1 = petMapper.batchSelectByIds(petIds, Pet.STATE_ONLINE);
        pets1.forEach(pet -> {
            pet.setStore_number(pet.getStore_number()+1);
            petMapper.update(pet);
        });
        // 调用支付宝的关单处理
        try {
            AlipayTradeCloseResponse close = Factory.Payment.Common().close(sn);
            System.out.println("支付宝关单" + close);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 删除定时任务
        QuartzUtil.removeJob(scheduler, sn);


    }
}
